package com.sunjob.mroresumeproject.busresumecontroller;

import com.sunjob.common.domain.mroresume.BusAction;
import com.sunjob.common.domain.mroresume.CKBus;
import com.sunjob.common.domain.mroresume.RPParts;
import com.sunjob.common.result.Result;
import com.sunjob.mroresumeproject.service.BusActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/BusAction")
@CrossOrigin
public class BusActionController {

    @Autowired
    BusActionService busActionService;


    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2){

        int page2=Integer.parseInt(pages2);
        if (page2<1)
        {
            page2=1;

        }

        int size=5;
        //部件定检履历查出部件定检记录总条数
        int count=busActionService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;

        if(page2>pageCount)
        {
            page2=pageCount;
        }

        //第一页放五条数据
        int pages=(page2-1)*size;
        List<BusAction> list=busActionService.queryBusAction(pages,size);


        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }

    //点击机车运行履历菜单查询
    @RequestMapping("/selectBusAction")
    public Result selectBusAction(){
        int page2=1;
        int size=5;

        //机车运行履历查出机车运行记录总条数
        int count=busActionService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<BusAction> list=busActionService.queryBusAction(pages,size);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectBusActioncontent")
    public Result selectBusActioncontent(@RequestParam(value = "busid",required = false) String busid){
        //搜索框条件查询可以是机车编号或者机车型号
        List<BusAction> list=busActionService.queryBusAction2(busid);

        //result返回查询出的机车运行履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }

    //新增按钮，机车运行履历新增
    @RequestMapping("/insertBusAction")
    public void insertBusAction(BusAction busAction){
        //机车运行履历记录新增
        busActionService.insertBusAction(busAction);


    }

    //编辑按钮，根据busid查询出当前id的数据
    @RequestMapping("returnBusAction")
    public Result returnBusAction(@RequestParam("busid")Long busid){
        List<BusAction> busAction=busActionService.queryBusActionone(busid);
        Result result=new Result();
        result.setData(busAction);
        return result;
    }

    //对根据busid查询出的机车运行履历数据做修改
    @RequestMapping("/updateBusAction")
    public void updateBusAction(BusAction busAction){

        busActionService.updateBusAction(busAction);

    }

    //根据busid删除修改这条记录的状态
    @RequestMapping("/deleteBusAction")
    public void deleteBusAction(@RequestParam("busid")String busid){
        //修改这条记录状态，0为消失，1为存在
        busActionService.deleteBusAction(busid);

    }

    @RequestMapping("/deleteBusAction2")
    public void deletetransform2(String busid2){
        System.out.println(busid2);
        String[] a= busid2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            busActionService.deleteBusActionone(a[i]);
        }
    }

}
